//
//  SetPreferentialClassTypeController.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/22.
//

#import "SetPreferentialClassTypeController.h"
#import "OneTabButView.h"
#import "PGDatePickManager.h"
#import "CoachsBookInputModel.h"
#import "CoachsBookTableHeaderCell.h"
#import "CoachsBookTextFieldCell.h"
#import "CoachsBookSwitchCell.h"
#import "ClassTypeTableViewCell.h"

@interface SetPreferentialClassTypeController ()<UITableViewDelegate,UITableViewDataSource,OneTabButViewDelegate,UITextFieldDelegate,PGDatePickerDelegate>
@property(nonatomic, strong)NSMutableArray *dataSource;

@end

@implementation SetPreferentialClassTypeController

-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navgationheadv.titlelabel.text = @"班型优惠";
    
    OneTabButView *tabbutview = [[OneTabButView alloc]initWithFrame:CGRectMake(0, APPheight-SafeAreaBottomHeight-65, APPwidth, 65)];
    tabbutview.delegate = self;
    [tabbutview.tabbut setTitle:@"确定" forState:UIControlStateNormal];
    [self.view addSubview:tabbutview];
    
    [self getData];
}

- (void)getData
{
    NSArray *titlearr = @[];
    NSArray *promptarr = @[];
    NSArray *celltypearr = @[];
    titlearr = @[@"活动班型价格",@"活动开始时间",@"活动结束时间",@"该活动结束后将自动续期"];
    promptarr = @[@"请输入您的活动价格",@"请选择时间",@"请选择时间",@""];
    celltypearr = @[@"1",@"1",@"1",@"5"];
    for (NSInteger i=0; i<titlearr.count; i++) {
        CoachsBookInputModel *model = [[CoachsBookInputModel alloc]init];
        model.title = titlearr[i];
        model.prompt = promptarr[i];
        model.cellType = [celltypearr[i] integerValue];
        model.showArrow = YES;
        if (i==0||i==3) {
            model.showArrow = NO;
        }
        [self.dataArray addObject:model];
        [self.dataSource addObject:model];
    }
    
}

- (void)setupUI {
    [super setupUI];
    self.tableView.frame = CGRectMake(0, SafeAreaTopHight, APPwidth, APPheight-SafeAreaTopHight-SafeAreaBottomHeight-65);
    UIView *HeaderV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 45)];
    HeaderV.backgroundColor = [UIColor whiteColor];
    
    UIView *myDataLineV = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 3, 15)];
    myDataLineV.backgroundColor = rgba(22, 120, 255, 1);
    myDataLineV.layer.cornerRadius = 2;
    [HeaderV addSubview:myDataLineV];
    UILabel *myDataLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myDataLineV.frame)+5, 15, 150, 15)];
    myDataLab.text = @"详细信息";
    myDataLab.textColor = [UIColor blackColor];
    myDataLab.textAlignment = 0;
    myDataLab.font = kPingFangSemibold(16);
    [HeaderV addSubview:myDataLab];
    
    self.tableView.tableHeaderView = HeaderV;
}



#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return self.dataArray.count;
    }
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        return 50;
    }
    return 125;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        CoachsBookInputModel *model = self.dataArray[indexPath.row];
        if (model.cellType == 1) {
            CoachsBookTextFieldCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTextFieldCell"];
            if (!cell) {
                cell=[[CoachsBookTextFieldCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTextFieldCell"];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell cellFillWithModel:model];
            cell.contentField.tag = indexPath.row;
            cell.contentField.delegate = self;
            return cell;
        }
        else if (model.cellType == 5)
        {
            CoachsBookSwitchCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookSwitchCell"];
            if (!cell) {
                cell=[[CoachsBookSwitchCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookSwitchCell"];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell cellFillWithModel:model];
            [cell.SwitchBut addTarget:self action:@selector(SwitchButCheng:) forControlEvents:UIControlEventValueChanged];
            cell.SwitchBut.tag = indexPath.row;
            return cell;
            
        }
    }
    ClassTypeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ClassTypeTableViewCell"];
    if (!cell) {
        cell=[[ClassTypeTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ClassTypeTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.chooseBut addTarget:self action:@selector(chooseButCheng:) forControlEvents:UIControlEventTouchUpInside];
    cell.chooseBut.tag = indexPath.row;
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section ==0) {
        return 0.01;
    }
    return 45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section ==0) {
        return [UIView new];
    }
    UIView *HeaderV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 45)];
    HeaderV.backgroundColor = [UIColor whiteColor];
    
    UIView *myDataLineV = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 3, 15)];
    myDataLineV.backgroundColor = rgba(22, 120, 255, 1);
    myDataLineV.layer.cornerRadius = 2;
    [HeaderV addSubview:myDataLineV];
    UILabel *myDataLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myDataLineV.frame)+5, 15, 150, 15)];
    myDataLab.text = @"请选择班型";
    myDataLab.textColor = [UIColor blackColor];
    myDataLab.textAlignment = 0;
    myDataLab.font = kPingFangSemibold(16);
    [HeaderV addSubview:myDataLab];
    return HeaderV;
}

#pragma mark -活动结束后是否自动续期
-(void)SwitchButCheng:(UISwitch *)but
{
    CoachsBookInputModel *model = self.dataArray[but.tag];
    if (but.on) {
        model.content = @"1";
    }
    else{
        model.content = @"0";
    }
}

#pragma mark -选择优惠班型（单选)
-(void)chooseButCheng:(UIButton *)but
{
    but.selected = !but.selected;
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:( UITextField*)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (model.showArrow) {
        UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        textField.inputView = dummyView; // Hide keyboard,but show blinking cursor
        if (textField.tag == 1 || textField.tag == 2) {
            PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
            datePickManager.isShadeBackground = true;
            datePickManager.cancelButtonTextColor = rgba(153, 153, 153, 1);
            datePickManager.confirmButtonTextColor = rgba(22, 120, 255, 1);
            datePickManager.headerViewBackgroundColor = [UIColor whiteColor];
            PGDatePicker *datePicker = datePickManager.datePicker;
            datePicker.tag = textField.tag;
            datePicker.delegate = self;
            datePicker.datePickerType = PGDatePickerTypeVertical;
            datePicker.isHiddenMiddleText = false;
            datePicker.datePickerMode = PGDatePickerModeDate;
            [self presentViewController:datePickManager animated:false completion:nil];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            dateFormatter.dateFormat = @"yyyy-MM-dd";
            NSDate *date = [dateFormatter dateFromString: model.content];
            [datePicker setDate:date animated:true];
        }
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (textField.text.length) {
        model.content = textField.text;
    }
}


#pragma mark - PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    NSLog(@"dateComponents = %@", dateComponents);
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-d";
    NSDate *time = [formatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%ld",[dateComponents year],[dateComponents month],[dateComponents day]]];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *daystr = [formatter stringFromDate:time];
    NSLog(@"%@",daystr);
    
    if (datePicker.tag == 1) {
        CoachsBookInputModel *endmodel = self.dataArray[2];
        if (![LongHelper isBlankString:endmodel.content]) {
            if ([LongHelper timeWithYearMonthDayCountDown:daystr AndTiem:endmodel.content]>=0) {
                //开始时间不可以大于结束时间
                [SVProgressHUD showErrorWithStatus:@"开始时间不可以大于结束时间"];
                [self.view endEditing:YES];
                return;
            }
        }
        
    }
    
    if (datePicker.tag == 2) {
        CoachsBookInputModel *startmodel = self.dataArray[1];
        if (![LongHelper isBlankString:startmodel.content]) {
            if ([LongHelper timeWithYearMonthDayCountDown:startmodel.content AndTiem:daystr]>=0) {
                //结束时间不可以小于开始时间
                [SVProgressHUD showErrorWithStatus:@"结束时间不可以小于开始时间"];
                [self.view endEditing:YES];
                return;
            }
        }
        
    }
    

    
    CoachsBookInputModel *model = self.dataArray[datePicker.tag];
    model.content = daystr;
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:datePicker.tag inSection:0];
    CoachsBookTextFieldCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.contentField.text = daystr;
    [self.view endEditing:YES];
    
    
}

-(void)cancelDatePicker:(PGDatePicker *)datePicker
{
    [self.view endEditing:YES];
}

#pragma mark -OneTabButViewDelegate(提交)
- (void)OneTabButViewBtnSelectedDelegate:(NSInteger)tag
{
    
}


@end
